<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use App\Services\LoginService;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Validator;

class LoginController extends Controller
{
    /**
     * @发送验证码
     * created 在 2021/11/11 16:49 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendCode(Request $request)
    {
        $result = SELF::checkMobile($request->all());//校验手机号

    	if($result == '') {
    		return response()->json(SmsService::sendCode($request->mobile));
    	}
    	else {
    		return response()->json([
					'code' => 1,
					'msg' => $result,
					'data' => ''
				]);
    	}
    }

    //校验手机号
    public function checkMobile($data) {
        $messages = [
            'mobile.required' => '手机号不能为空，请输入手机号',
            'mobile.regex' => '手机号格式错误，请重试',
        ];

        $validator = Validator::make($data, [
            'mobile' => 'required|regex:/^1[3456789][0-9]{9}$/',
        ], $messages);

        return $validator->errors()->first();
    }

    /**
     * @登录
     * created 在 2021/11/11 16:49 创建
     * @author SunWu
     * @param Request $request
     * @param $request->device //登录端：1-安卓 2-ios 3-pc 4-微信小程序 5-抖音小程序
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $validator = Validator::make($data = $request->all(), [
                            'device' => [
                                'required',
                                'integer',
                                Rule::In([1,2,3,4,5]),
                            ],//物料类型所属二级分类id
                        ],
                        [
                            'device.required' => '参数`device`缺失',
                            'device.integer' => '参数`device`只能为数字',
                            'device.in' => '参数`device`不合法',
                        ]
                    );

        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $minicode = empty($request->mini_code)?'':$request->mini_code;

        if(in_array($request->type, [0, 1, 2])) {
            //type:0->验证码登录(主账号);1->密码登录(子账号);2->主账号密码登录
            switch($request->type) {
                case 0:

                    //默认验证码登录
                    if($request->code == 666666) {
                        return response()->json(
                            LoginService::defaultCodeLog(
                                $data,
                                empty($request->device_id)?0:$request->device_id,
                                $minicode
                            )
                        );
                    }

                    $result = SELF::checkMobile($request->all());//校验手机号

                    if(!$result == '') {
                        return response()->json([
                                'code' => 1,
                                'msg' => $result,
                                'data' => ''
                            ]);
                    }

                    $result = LoginService::mainLogin(
                        $request->mobile,
                        $request->code,
                        $request->device,
                        $minicode,
                        empty($request->bind_id)?0:$request->bind_id,
                        empty($request->device_id)?0:$request->device_id
                    );
                    break;
                case 1:
                    if(empty($request->username)) {
                        return response()->json([
                                'code' => 1,
                                'msg' => '请输入用户名',
                                'data' => ''
                            ]);
                    }

                    if(empty($request->password)) {
                        return response()->json([
                                'code' => 1,
                                'msg' => '请输入密码',
                                'data' => ''
                            ]);
                    }

                    $result = LoginService::userLogin(
                        $request->username,
                        $request->password,
                        $request->device,
                        empty($request->device_id)?0:$request->device_id,
                        $minicode
                    );
                    break;
            }

            return $result;
        }
        else {
            return response()->json([
					'code' => 1,
					'msg' => '参数`type`不合法',
					'data' => ''
				]);
        }
    }
}
